package com.lethelh.reflect;

import com.lethelh.reflect.entity.Goods;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 反射:商品测试类
 */
public class GoodsSample {
    public static void main(String[] args) {
        try {
            Class goodsClass = Class.forName("com.lethelh.reflect.entity.Goods");
            Constructor goodsConstructor = goodsClass.getConstructor(new Class[]{
                    Integer.class, String.class, Float.class, String.class
            });
            Goods goods = (Goods)goodsConstructor.newInstance(new Object[] {
                    27, "Mac电脑", 9800f, "M1芯片"
            });
            System.out.println(goods);
            Method display = goodsClass.getMethod("display", new Class[]{
                    String.class
            });
            display.invoke(goods, new Object[] {"配备苹果公司最新的M1处理器"});
            System.out.println(goods);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}
